package top.jnaw.jee.utils.nim;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class NIMMsg {

  private static final String URL_SEND_JSON = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
  private static final String URL_SEND_NOTIFY = "https://api.netease.im/nimserver/msg/sendMsg.action";
  private static final String URL_BROADCAST_JSON = "https://api.netease.im/nimserver/msg/broadcastMsg.action";
  private static final String URL_SEND_BATCH_JSON = "https://api.netease.im/nimserver/msg/sendBatchAttachMsg.action";

  public static boolean p2p(String from, String to, JSON data) {
    return send(from, to, true, data);
  }

  public static boolean group(String from, String to, JSON data) {
    return send(from, to, false, data);
  }

  public static boolean send(String from, String to, boolean p2p, JSON data) {
    Map<String, String> params = new HashMap<>();
    params.put("from", from);
    params.put("to", to);
    params.put("msgtype", p2p ? "0" : "1");
    params.put("attach", data.toJSONString());

    JSONObject res = Utils.request(URL_SEND_JSON, params);

//    Log.d("send json: " + res);
    return res.getIntValue("code") == 200;
  }

//  public static boolean sendBatch(Users users, String toAccids,
//      JSONObject data) {
//    Map<String, String> params = new HashMap<>();
////    params.put("fromAccid", users.getUsername());
//    params.put("toAccids", toAccids);
//    params.put("attach", data.toJSONString());
//    JSONObject res = Utils.request(URL_SEND_BATCH_JSON, params);
//    JSONObject object = data.getJSONObject("data");
////    Log.d("send json: " + res);
////    MessageHistory.add("",
////        0, users.getId(),
////        res.getIntValue("code"), toAccids,
////        object.getString("content"),
////        object.getString("title"),
////        (Map) object);
//    return res.getIntValue("code") == 200;
//  }

  // [Neo] .TODO
  public static boolean broadcast(String from, String body) {
    Map<String, String> params = new HashMap<>();
    params.put("from", from);
    params.put("body", body);

    JSONObject res = Utils.requestSMS(URL_BROADCAST_JSON, params);

//    Log.d("send json: " + res);
    return res.getIntValue("code") == 200;
  }

}
